// patrolnpc.txt
// Creature attacks anything it hates nearby, and spends the rest of its time patrolling a path.
// Once it has won, it returns to its home.
// Memory Cells:
//   Cell 0 - Attack type.
//	 Cell 1 - If 0, just loops around path. If 1, walks to end of path, waits a little, and returns home.
//      If 2, goes to end of path and just stops.
//   Cell 2 - Node if talked to. 0 means no conversation
//   Cell 3,4 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.

begincreaturescript;

variables;

short i,target;
short last_abil;
short ae_range = 7;
short going = 0;
short charge = 0;

body;

beginstate INIT_STATE;
	set_name(ME,"Core Golem");
	set_level(ME,30);
	set_boss_level(ME,2);
	set_walk_speed(ME,16);
	set_char_unkillable(ME,1);
	
	set_resistance(ME,0,100);
	set_resistance(ME,1,100);
	set_resistance(ME,2,100);
	set_resistance(ME,3,100);
	set_resistance(ME,4,100);
	set_resistance(ME,5,100);
	set_resistance(ME,6,100);
	
	last_abil = get_current_tick();
	
	break;

beginstate DEAD_STATE;

break;

beginstate START_STATE;
	if (gf(56,20) != 1) {
		set_attitude(ME,4);
		heal_char(ME,1000);

		if (my_dist_from_start() > 1)
			return_to_start(ME,1);

		end();
		}
		
	if ((is_combat()) && (get_attitude(ME) < 10)) {
		while (my_ap() > 2) {
			deduct_ap(1);
			}
		}

	if ((going == 0) && (get_health(ME) < (get_max_health(ME) * 3) / 4) ) {
		charge = 0;
		going = 1;
		print_named_str(ME,"has taken too much damage. It withdraws.");
		if (gf(56,23) == 0) {
			sf(56,23,1);
			begin_talk_mode(11);
			}
		}
	if ((going == 1) && (get_health(ME) >= get_max_health(ME) - 50)) {
		going = 0;
		print_named_str(ME,"has repaired itself. It starts to walk again.");
		}
		
	if ((going == 0) && (dist_to_nav_point(ME,3) <= 2) &&  
	  (tick_difference(last_abil,get_current_tick()) > 0)) {
		last_abil = get_current_tick();
		
		if ((charge > 3) && ((is_combat()) || (get_ran(1,0,1) == 0))) {
			run_char_animation(2,1,35);	

		  	place_particle_num(ME,2,4,8);
			print_named_str(ME,"radiates stored energy.");
			pc_heard_sound_delay(117,250);						
	  		create_missile_spiral(154,20,10,2);
	  		damage_nearby(get_ran(1,200,250),10,2,0);
	  		
	  		if (gf(56,24) == 0) {
				sf(56,24,1);
				begin_talk_mode(12);
				}

			}
			else {
		  		place_particle_num(ME,2,4,8);
				print_named_str(ME,"draws power from the pylon.");
				charge = charge + 1;
				if (gf(56,22) == 0) {
					sf(56,22,1);
					begin_talk_mode(10);
					}
				}

		end();
		}
	if ((going == 1) && (my_dist_from_start() <= 1) &&  
	  (tick_difference(last_abil,get_current_tick()) > 0)) {
		last_abil = get_current_tick();
		run_char_animation(2,1,35);	
		heal_char(ME,get_max_health(ME) / 8);
		print_named_str(ME,"repairs itself.");
		pc_heard_sound_delay(100,250);						
		end();
		
		}
		
	if (going == 0) {
		if (dist_to_nav_point(ME,3) > 2)
			approach_zone_loc(ME,get_memory_cell(0),get_memory_cell(1),1);
		}
		else {
			if (my_dist_from_start() > 1)
				return_to_start(ME,1);
				
			}
			
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;


beginstate TALKING_STATE;
		print_str("Talking: The golem doesn't respond. It is dormant.");
break;